export default{

    initHtml(ScreenData={},param,callback){
        
        var widthID = param.cpname+"_width"
        var gridID = param.cpname+"_table"
        var pageID = param.cpname+"_page"
        var formID = param.cpname+"_search"
        var html =  `<div class="widget-box" style="width: 100%;height:100%">
                        <div id="`+widthID+`" class="widget-body" >
                            <table id="`+gridID+`"></table>
                            <div id="`+pageID+`"></div>
                        </div>
                    </div>`
        $(param.menuBoxId).append(html)
        
        callback();
    },

    initJqgrid(data=[],param,callback){
        var searchFormId = "#"+param.cpconnect+"";
        var widthID = "#"+param.cpname+"_width"
        var gridID = "#"+param.cpname+"_table"
        var pageID = "#"+param.cpname+"_page"

        var height = $(widthID).parent("div").parent("div").height()-42-55-21
        $(gridID).jqGrid('GridUnload');
        var component_grid = data;
        var selectname = param['selectname'] || 'external';
        var procedure = param.procedurename;
        var str = getSqlText(searchFormId,selectname);
        if(selectname=="external"){
            str.arg.tablename = param.tablename
            str.arg.isboot = param.isboot || '0'
            str.arg.fields = param.fields
            str.arg.procedurename = param.procedurename
            str.arg.ArgTableName = 'external'
            str.arg.selectname = param.cpconnect
            str.arg.gridname = param.cpname
        }

        
        var grid_selector = $(gridID);
        var width = $(widthID).width();
        
        var colNames = []
        var colModel = []
        var sort = ""
        var rows = "-1"
        if(component_grid.length>0){
            var colNames_colModel = jqgrid_col_add(colNames,colModel,component_grid);
            colNames = colNames_colModel.colNames
            colModel = colNames_colModel.colModel
            sort = colNames_colModel.sort
            if(sort!=""){
                rows = "20"
            }
        }
        
        var fields_width = 0
        $.each(component_grid,function(key,value){
            fields_width = Number(value["width"])+fields_width
        })
        var shrinkToFit = false
        if(fields_width<width){
            shrinkToFit = true
        }
        
        grid_selector.jqGrid({
            url :$.HSAPI_AJAXURL+"/qms/mobile/procedure2", 
            datatype : "json",
            width :width,
            height : height,//100
            postData : $.HSAPI_ASKJSONINFO( JSON.stringify(str),procedure,'[{"page":1,"sord":"asc","rows":'+rows+',"sidx":"'+sort+'"}]',"listWithPage"),
            colNames : colNames,
            colModel : colModel, 
            sortname : sort, //sortby
            sortorder : "asc", 
            viewrecords : true,
            rowNum : rows,
            rowList:[10, 20, 30 ], 
            scroll:true,
            pager : $(pageID),
            shrinkToFit : shrinkToFit,
            footerrow : true,
            ondblClickRow: function(rowid){
                ins_argjson.rowDate =  $(gridID).jqGrid("getRowData",rowid);

                $("#"+param.cpname+"_dblclick").trigger("click",ins_argjson.rowDate)
            }, 
            onSelectRow:function(rowid){
                ins_argjson.rowDate =  $(gridID).jqGrid("getRowData",rowid);

                $("#"+param.cpname+"_click").trigger("click",ins_argjson.rowDate)
            },
            loadComplete : function(data){
                var ids = $(gridID).jqGrid('getDataIDs');
                if(ids.length>0){
                    $(gridID).jqGrid('setSelection',ids[0]);
                }
                
                $(gridID).find("td[role='gridcell']").each(function(){
                    if($(this).text()=="undefined"){
                        $(this).text("")
                    }
                })
                
                $(gridID).find(".need_statistics").each(function(){
                    // debugger
                    var id = $(this).attr("aria-describedby").replace(gridID.replace("#","")+"_","")
                    var quantity=jQuery(gridID).getCol(id,false,'sum').toFixed(2);
                    var json = {}
                    json[id] = quantity
                    jQuery(gridID).footerData("set",json)
                })
                
                //显示TIP
                showTip()
            }, 
        });
    },

}


